<?
@session_start();

require_once("conn.php");

$id = $_REQUEST["id"];
//echo $id;
$sql = "SELECT place,date(last_update) as dtt,order_by,DATE_ADD(date(last_update), INTERVAL 1 DAY) AS Tomorrow FROM pch_orderexport WHERE order_id='".$id."' GROUP BY place";
$query = mysql_query($sql);
$qq = mysql_query($sql." limit 0,1;");
$rdd = mysql_fetch_assoc($qq);
?>
<a href="exportpdf.php?id=<?=$id?>">Download PDF</a>
<?
while($row = mysql_fetch_array($query)){
	$sql = "SELECT * FROM pch_orderexport WHERE order_id='".$id."' AND place='".$row["place"]."';";
	//echo $sql;
	$qry = mysql_query($sql);
	$sql = "SELECT * FROM pch_dealer WHERE id='".$row["place"]."';";
		$qr = mysql_query($sql);
		$rd = mysql_fetch_assoc($qr);
	?>
	<table border="1" cellpadding="0" cellspacing="0" width="725">
		<tr>
			<td>To : <?=$rd["dealer"]?></td>
			<td>Date Order : <?=$rdd["dtt"]?></td>
		</tr>
		<tr>
			<td>Tel : <?=$rd["tel"]?></td>
			<td>Order By : <?=$rdd["order_by"]?></td>
		</tr>
		<tr>
			<td>Fax : <?=$rd["fax"]?></td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td>From : Banraya</td>
			<td>Order Receive : <?=$rdd["Tomorrow"]?></td>
		</tr>
		<tr>
			<td>Tel : 076-224439</td>
			<td>Receive By : <?=$rdd["order_by"]?></td>
		</tr>
		<tr>
			<td>Fax : 076-210516</td>
			<td>Receive Time : 07:00 AM</td>
		</tr>
		<tr>
			<th colspan="3">รายการสั่งซื้อ</th>
		</tr>
		<tr>
			<td colspan="2">
				<table border="1" cellpadding="0" cellspacing="0" width="100%">
					<tr>
						<th width="60">ลำดับที่</th>
						<th width="210">รายการ</th>
						<th width="60">จำนวน</th>
						<th width="60">หน่วย</th>
						<th width="60">ลำดับที่</th>
						<th width="210">รายการ</th>
						<th width="60">จำนวน</th>
						<th width="60">หน่วย</th>
					</tr>
	<?
		while($rs = mysql_fetch_array($qry)){
			$pro_name[] = $rs["product_name"];
			$pro_amount[] = $rs["amount"];
			$pro_unit[] = $rs["unit"];
		}
		$index=1;
		for($i=0;$i<count($pro_name);$i+=2){
			?>
				<tr>
					<td align="center"><?=$index?></td>
					<td><?=$pro_name[$i]?></td>
					<td align="center"><?=$pro_amount[$i]?></td>
					<td align="center"><?=$pro_unit[$i]?></td>
			
					<td align="center"><?=$index+ceil(count($pro_name)/2)?></td>
					<td><?=$pro_name[$i+1]?></td>
					<td align="center"><?=$pro_amount[$i+1]?></td>
					<td align="center"><?=$pro_unit[$i+1]?></td>
				</tr>
			<?
			$index++;
		}
		unset($pro_name);
		unset($pro_amount);
		unset($pro_unit);
		
	?>
				</table>
			</td>
		</tr>
	</table><br>
	<?
}
?>
